﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Edu.Web.Biz;
using Edu.Web.Data.Entity;
using Edu.Web.Data.Service;
using ESoft.Core.Utils.Extensions;

namespace Edu.Web.Controllers
{
    /// <summary>
    /// 操行评价
    /// </summary>
    public class EvaluateController : BaseController
    {
        private readonly IClazzService _clazzService;
        private readonly IStudentService _studentService;
        private readonly IEvaluateService _evaluateService;
        private readonly ITimeTableService _timeTableService;
        private readonly IUserService _userService;

        public EvaluateController(IClazzService clazzService, IStudentService studentService, IEvaluateService evaluateService, ITimeTableService timeTableService, IUserService userService)
        {
            _clazzService = clazzService;
            _studentService = studentService;
            _evaluateService = evaluateService;
            _timeTableService = timeTableService;
            _userService = userService;
        }

        /// <summary>
        /// 教师评价学生
        /// </summary>
        /// <returns></returns>
        public ActionResult Create4Teacher()
        {
            return View();
        }


        /// <summary>
        /// 教师评价学生
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult CreateEva4Teacher(EvaluateEntity evaluateVO)
        {
            try
            {
                if (evaluateVO.ClazzId==0)
                {
                    throw new Exception("请选择课程！");
                }
                if (evaluateVO.StuUserId==0)
                {
                    throw new Exception("请选择学生！");
                }
                if (evaluateVO.Remark.IsNullOrWhiteSpace())
                {
                    throw new Exception("请输入评语！");
                }

                evaluateVO.TeacherUserId = CurrentUser.UserId;
                evaluateVO.EvalType = 2;
                evaluateVO.CreateTime = DateTime.Now;
                var result = _evaluateService.Add(evaluateVO);
                return Content(result.Id.ToString());
            }
            catch (Exception ex)
            {
                return Content("err:" + ex.Message);
            }
        }

        /// <summary>
        /// 教师评价学生
        /// </summary>
        /// <returns></returns>
        public ActionResult List4Teacher()
        {
            return View();
        }

        /// <summary>
        /// 学生评价教师
        /// </summary>
        /// <returns></returns>
        public ActionResult Create4Student()
        {
            return View();
        }

        /// <summary>
        /// 学生评价教师
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Create4Student(EvaluateEntity evaluateVO)
        {
            try
            {
                evaluateVO.StuUserId = CurrentUser.UserId;
                evaluateVO.EvalType = 1;
                evaluateVO.CreateTime = DateTime.Now;
                var result = _evaluateService.Add(evaluateVO);
                return Content(result.Id.ToString());
            }
            catch (Exception ex)
            {
                return Content("err:" + ex.Message);
            }
        }

        /// <summary>
        /// 教师评价学生
        /// </summary>
        /// <returns></returns>
        public ActionResult List4Student()
        {
            return View();
        }

        /// <summary>
        /// 获取评价列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetAllEvalute4Teacher()
        {
            return Json(_evaluateService.GetListByTeacherId(CurrentUser.UserId), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取班级列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetAllClazzEntity()
        {
            return Json(_clazzService.LoadClazzByUserId(CurrentUser.UserId, 2), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 根据班级ID获取学生列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public JsonResult GetStudentListByClazzId(long clazzId)
        {
            return Json(_studentService.LoadStudentListByClazzId(clazzId), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult GetCourceList4Student()
        {
            return Json(_timeTableService.GetCourceProjectListByStuId(CurrentUser.UserId), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public ActionResult GetTeacherListByCourceId(long courceId)
        {
            return Json(_userService.GetTeacherListByCourceId(courceId), JsonRequestBehavior.AllowGet);
        }
    }
}